package cn.sc.allinpay.allfavour.business.login;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import cn.pai.mvp.callback.MpCallBack;
import cn.sc.allinpay.allfavour.base.connect.http.AppRequest;
import cn.sc.allinpay.allfavour.base.connect.http.RequestManager;
import cn.sc.allinpay.allfavour.base.model.BaseModel;
import cn.sc.allinpay.allfavour.business.common.data.CashierData;
import cn.sc.allinpay.allfavour.business.common.data.UserInfo;

/**
 * 登录
 * author：pany
 * on 2017/12/5 14:44
 */
public class LoginModel extends BaseModel {

    public void reqLogin(final String account, final String psw, final MpCallBack<CashierData, String> mpCallBack) {
        RequestManager.reqLogin(account, psw, new AppRequest.RequestCall() {
            @Override
            protected void onSuccess(JSONObject response) {
                CashierData cashierData = JSON.parseObject(response.getString("data"), CashierData.class);
                cashierData.setCashierNo(account);
                cashierData.setPassword(psw);
                mpCallBack.onSuccess(cashierData);
            }

            @Override
            public void onFailure(JSONObject response, String message) {
                mpCallBack.onFailure(response != null ? response.toString() : null, message);
            }
        });
    }

    public void posLogin(final String orgId, final String userId, final String pwd, final String posCode, final String terminalCode, final MpCallBack<UserInfo, String> mpCallBack) {
        RequestManager.posLogin(orgId, userId, pwd, posCode, terminalCode, new AppRequest.PosRequestCall() {
            @Override
            protected void onSuccess(JSONObject response) {
                UserInfo userInfo = JSON.parseObject(response.getString("data"), UserInfo.class);
                mpCallBack.onSuccess(userInfo);
            }

            @Override
            public void onFailure(JSONObject response, String message) {
                mpCallBack.onFailure(response != null ? response.toString() : null, message);
            }
        });
    }
}
